﻿namespace CardManagement
{
    using System;
    using System.IO;
    using System.Text;

    public class FileHelper
    {
        public static bool CreateDir(string dirPath)
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CreateFile(string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool DeleteFile(string FilePathName)
        {
            bool flag2;
            try
            {
                FileInfo info = new FileInfo(FilePathName);
                if (!info.Exists)
                {
                    flag2 = false;
                }
                else
                {
                    info.Delete();
                    flag2 = true;
                }
            }
            catch
            {
                flag2 = false;
            }
            return flag2;
        }

        public static string GetFileName(string fileurl)
        {
            string str;
            if (fileurl == null)
            {
                str = "";
            }
            else
            {
                FileInfo info = new FileInfo(fileurl);
                str = !info.Exists ? "" : info.Name;
            }
            return str;
        }

        public static long GetFileSize(string fileurl)
        {
            long num2;
            if (fileurl == null)
            {
                num2 = 0L;
            }
            else
            {
                FileInfo info = new FileInfo(fileurl);
                num2 = !info.Exists ? 0L : (info.Length / 0x400L);
            }
            return num2;
        }

        public static string GetFileType(string fileurl)
        {
            string str;
            if (fileurl == null)
            {
                str = "";
            }
            else
            {
                FileInfo info = new FileInfo(fileurl);
                str = !info.Exists ? "" : info.Extension.Replace('.', ' ').Trim();
            }
            return str;
        }

        public static bool IsExist(string filePath) => 
            File.Exists(filePath);

        public static string TxtRead(string path)
        {
            StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
            StringBuilder builder = new StringBuilder();
            while (true)
            {
                if (reader.EndOfStream)
                {
                    reader.Close();
                    return builder.ToString();
                }
                builder.AppendLine(reader.ReadLine());
            }
        }

        public static void TxtWrite(string path, string contents)
        {
            FileStream stream = new FileStream(path, FileMode.Create);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            writer.Write(contents);
            writer.Flush();
            writer.Close();
            stream.Close();
        }
    }
}

